package objects;

import java.awt.Graphics;
import java.awt.Graphics2D;

import sfx.SoundManager;
import core.Main;
import core.Vector2;

/**
 * A Mine. Explodes on collision with a robot.
 * 
 * @author robert.kraneis
 */
public class Mine extends Entity implements Item {
	
	public static final int SIZE = 20;
	public static final int EXPLOSIONSIZE = 200;
	
	private int itemID = 0;
	
	protected long triggerTime;
	protected long placeTime;
	protected boolean active;
	protected boolean triggered;
	
	public Mine(Vector2 pos) {
		super(pos, 0, new Vector2(SIZE, SIZE));
		triggerTime = -1;
		active = false;
		triggered = false;
		placeTime = System.currentTimeMillis();
	}
	
	@Override
	public void render(Graphics g) {

		Graphics2D g2d = (Graphics2D)g;
		
		int x = (int)((pos.x - Main.mapPos.x) * Main.zoom);
		int y = (int)((pos.y - Main.mapPos.y) * Main.zoom);
		
		if (active) {
			g2d.drawImage(Main.imageLoader.mineOn, (int)(x - SIZE * Main.zoom / 2), (int)(y - SIZE * Main.zoom / 2), (int)(SIZE * Main.zoom), (int)(SIZE * Main.zoom), null);
		} else {
			g2d.drawImage(Main.imageLoader.mineOff, (int)(x - SIZE * Main.zoom / 2), (int)(y - SIZE * Main.zoom / 2), (int)(SIZE * Main.zoom), (int)(SIZE * Main.zoom), null);
		}
	}
	
	@Override
	public void tick() {
		if (!active && System.currentTimeMillis() - placeTime > 1500) {
			active = true;
		}
		
		if (active) {
			if (triggerTime == -1 && Main.entityCollision(this, pos) != null) {
				trigger();
			}
			if (triggerTime != -1 && System.currentTimeMillis() - triggerTime > 2000) {
				Explosion explosion = new Explosion(new Vector2(pos), EXPLOSIONSIZE);
				Main.addDrawable(explosion);
				Main.addTickable(explosion);
				
				Main.entityExplosionCollision(pos, EXPLOSIONSIZE / 2, 70);
				
				removed = true;
			}
		}
	}
	
	public void trigger() {
		triggerTime = System.currentTimeMillis();
		Main.soundManager.play(SoundManager.WTFBOOM);
	}
	
	@Override
	public int getID() {
		return itemID;
	}
	
}
